// AuthService.java - 修改初始化默认用户部分
package com.library.service;

import com.library.entity.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class AuthService {
    private Map<String, User> users; // username -> User
    private User currentUser;

    public AuthService() {
        this.users = new ConcurrentHashMap<>();
        this.currentUser = null;
        initializeDefaultUsers();
    }

    private void initializeDefaultUsers() {
        // 初始化默认管理员 - 修改为指定的管理员信息
        Admin admin = new Admin("ADMIN001", "admin", "2083658769Ybr", 
                               "yangborui@library.com", "杨博锐", "13800000000");
        users.put(admin.getUsername(), admin);

        // 初始化示例读者用户
        Reader sampleReader = new Reader("2023001", "张三", "男", "13800138000", 
                                        "zhangsan@email.com", "计算机学院", Reader.ReaderType.STUDENT);
        ReaderUser readerUser = new ReaderUser("zhangsan", "123456", 
                                             "zhangsan@email.com", sampleReader);
        users.put(readerUser.getUsername(), readerUser);
    }

    // 读者用户注册
    public boolean registerReader(String username, String password, String email, 
                                 String readerId, String name, String gender, 
                                 String phone, String department, Reader.ReaderType type) {
        if (users.containsKey(username)) {
            return false;
        }

        // 创建Reader实体
        Reader reader = new Reader(readerId, name, gender, phone, email, department, type);
        // 创建ReaderUser
        ReaderUser readerUser = new ReaderUser(username, password, email, reader);
        
        return register(readerUser);
    }

    // 新增管理员注册方法
    public boolean registerAdmin(String username, String password, String email,
            String adminId, String realName, String phone) {
    	if (users.containsKey(username)) {
            return false;
        }

        // 创建Admin
    	Admin admin = new Admin(adminId, username, password, email, realName, phone);
        return register(admin);
    }

    // 其他现有方法保持不变...
    public boolean login(String username, String password) {
        User user = users.get(username);
        if (user != null && user.getPassword().equals(password)) {
            this.currentUser = user;
            return true;
        }
        return false;
    }

    public boolean register(User user) {
        if (users.containsKey(user.getUsername())) {
            return false;
        }
        users.put(user.getUsername(), user);
        return true;
    }

    public void logout() {
        this.currentUser = null;
    }

    public User getCurrentUser() {
        return currentUser;
    }

    public boolean isUsernameExists(String username) {
        return users.containsKey(username);
    }

    public List<User> getAllUsers() {
        return new ArrayList<>(users.values());
    }
}